//
//  MessageHandViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/8/21.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class MessageHandViewController: NSViewController {
    
    var dataList:[HandInfo] = [] {
        didSet {
            let dowHeight = String.isHostId() ? 60:6
            let height = (dataList.count*44+dowHeight)
            let new = height>400 ? 400:height
            self.view.setFrameSize(NSSize.init(width: 300, height: new))
        }
    }
    
    var hasDown:Bool = true {
        didSet {
            self.downHandBtn.isHidden = !hasDown
            let k = hasDown ? -50:0
            self.tabView.snp.updateConstraints { (make) in
                make.bottom.equalTo(k)
            }
        }
    }
    
    override func loadView() {
        let view = PopoverContentView.init(frame: NSMakeRect(0, 0, 300, 500))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.white.cgColor
        self.view = view
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.loadData()
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        
        NotificationCenter.default.addObserver(self, selector: #selector(userStatuChange(not:)), name: MTUserStateNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(allDownHandUp(not:)), name: MTADownHandUpNotification, object: nil)
    }
    
    func loadData() {
        self.downHandBtn.isHidden = !String.isHostId()
        self.dataList = Td.hands
        self.tabView.reloadData()
        self.hasDown = String.isHostId()
    }
    
    func setupUI() {
        self.view.layer?.backgroundColor = color_gray(240).cgColor
        
        self.view.addSubview(tabView)
        tabView.snp.makeConstraints { (make) in
            make.top.centerX.width.equalToSuperview()
            make.bottom.equalTo(-50)
        }
        
        self.view.addSubview(downHandBtn)
        downHandBtn.snp.makeConstraints { (make) in
            make.width.equalTo(240)
            make.height.equalTo(32)
            make.bottom.equalTo(-10)
            make.centerX.equalToSuperview()
        }
    }
    lazy var downHandBtn: ZHButton = {
        let btn = ZHButton.init()
        btn.zh_title = "全体放手"
        btn.zh_font = NSFont.systemFont(ofSize: 14)
        btn.zh_color = NSColor.white
        btn.zh_backgroundColor = color_main108
        btn.target = self
        btn.action = #selector(downAllHand(senber:))
        return btn
    }()
    lazy var tabView: ZHTableView = {
        let tab = ZHTableView.init(frame: NSRect.zero)
        tab.delegate = self
        tab.dataSourse = self
        return tab
    }()
    
}

extension MessageHandViewController :ZHTableViewDelegate,ZHTableViewDataSource  {
    func zh_tableView(_ tableView: ZHTableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataList.count
    }
    
    func zh_tableView(_ tableView: ZHTableView, cellForRowAt indexPath: IndexPath) -> NSView? {
        var cell = tableView.tabView.makeView(withIdentifier: MessageHandCell.identifier, owner: self) as? MessageHandCell
        if cell == nil {
            cell = MessageHandCell.init(identifier: MessageHandCell.identifier) { (senber) in
                self.downAction(row: indexPath.item, senber: senber)
            }
            cell?.downBtn.isHidden = !String.isHostId()
        }
        cell?.stitle.stringValue = self.dataList[indexPath.item].name ?? "未知"
        return cell
    }
    
    func zh_tableView(_ tableView: ZHTableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44
    }
    
    
}

extension MessageHandViewController {
    @objc func downAllHand(senber:ZHButton) {
        IMcmd.shared.downAllHand()
        MTNot.postNotAllHandUp()
        self.dataList = Td.hands
        self.tabView.reloadData()
        
    }
    
    func downAction(row:Int,senber:ZHButton) {
        let info = self.dataList[row]
        if info.userid == nil {
            MTNot.postNotHandUp(isok: false)
        }else {
            //self.down(userid: info.userid)
            MTNot.postNotHandUp(isok: false,userid: info.userid)
            IMcmd.shared.downUserHand(userId: info.userid!)
        }
        self.dataList = Td.hands
        self.tabView.reloadData()
    }
    
    func down(userid:String?) {
        if userid == nil {
            
        }else {
            let index = Td.sh().usr.remote.firstIndex { (info) -> Bool in
                return info.userId == userid
            }
            if let inx = index {
                Td.sh().usr.remote[inx].isHandUp = false
            }
            
            let index2 = Td.sh().usr.adcs.firstIndex { (info) -> Bool in
                return info.userId == userid
            }
            if let inx2 = index2 {
                Td.sh().usr.adcs[inx2].isHandUp = false
            }
        }
    }
    
    
    @objc func allDownHandUp(not:Notification) {
        if not.object as? String == ReloadNotification {
            self.tabView.reloadData()
            self.view.window?.performClose(nil)
            return
        }
    }
    
    @objc func userStatuChange(not:Notification) {
        guard let mode = not.object as? UserMode else { return }
        if mode.isHandUp != nil {
            self.dataList = Td.hands
            self.tabView.reloadData()
            if self.dataList.count == 0 {
                self.view.window?.performClose(nil)
            }
        }
    }
}
